
package com.dyzx.hbb.security.listener;

import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AbstractAuthenticationEvent;
import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.security.authentication.event.InteractiveAuthenticationSuccessEvent;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

@Component
public class AuthenticationStateListener implements 
        ApplicationListener<AbstractAuthenticationEvent> {

    @Override
    public void onApplicationEvent(AbstractAuthenticationEvent event) {
        System.out.println("\n=== Authentication Event ===");
        System.out.println("Event type: " + event.getClass().getSimpleName());
        
        Authentication authentication = event.getAuthentication();
        System.out.println("Principal: " + authentication.getPrincipal());
        System.out.println("Authorities: " + authentication.getAuthorities());
        System.out.println("Details: " + authentication.getDetails());
        
        if (event instanceof AbstractAuthenticationFailureEvent) {
            Exception exception = ((AbstractAuthenticationFailureEvent) event).getException();
            System.out.println("Failure reason: " + exception.getMessage());
            exception.printStackTrace();
        }
        
        if (event instanceof InteractiveAuthenticationSuccessEvent) {
            System.out.println("Interactive authentication successful");
        }
        
        if (event instanceof AuthenticationSuccessEvent) {
            System.out.println("Authentication successful");
        }
        
        System.out.println("==============================\n");
    }
}